package com.leetcode.August;

import java.util.HashMap;

/**
 * @Description: 5. 最长回文子串
 * @Author: Mr.Fxy
 * @CreateTime: 2024-08-08 09:59
 */
public class LC5最长回文子串 {
    private int start; // 用来存储最长回文子串的起始位置
    private int len = 0;
    public String longestPalindrome(String s) {
        if (s == null || s.length() < 1) return "";
        for (int i = 0; i < s.toCharArray().length; i++) {
            findMax(s, i, i);
            findMax(s, i, i+1);//偶数
        }
        return s.substring(start,start+ len);

    }
    private void findMax(String s, int left, int right) {
        while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
            if (right - left + 1 > len){
                start=left;
                len=right-left+1;
            }
            left--;
            right++;
        }
    }

}
